<?php

include "../../lib/start.php";

check_session();
check_permission(ADM_PERM);

$sel_anni = "SELECT * FROM anni ORDER BY id_anno DESC";
try{
	$res_anni = $db->executeQuery($sel_anni);
} catch (MySQLException $ex){
    $ex->alert();
	exit;
}

if(isset($_REQUEST['id']) && $_REQUEST['id'] != 0){
    // modifica
    $sel_prj = "SELECT * FROM progetti WHERE id_progetto = ".$_REQUEST['id'];
    try{
    	$res_prj = $db->executeQuery($sel_prj);
    } catch (MySQLException $ex){
    	$ex->alert();
		exit;
	}
    $project = $res_prj->fetch_assoc();
	$teachers = split("#", $project['referenti']);
    $teacs = join(", ", $teachers);
    $sel_teachers = "SELECT utenti.nome, utenti.cognome FROM utenti, docenti WHERE utenti.uid = docenti.id_docente AND id_docente IN ($teacs) ORDER BY cognome, nome";
    try{
    	$res_teachers = $db->executeQuery($sel_teachers);
    } catch (MySQLException $ex){
    	$ex->alert();
		exit;
	}
    $teachers_string = array();
    while($ins = $res_teachers->fetch_assoc()){
    	array_push($teachers_string, $ins['cognome']." ".$ins['nome']);
    }
    // documenti in archivio per il progetto
    $sel_docs = "SELECT COUNT(*) FROM documents WHERE progetto = ".$_REQUEST['id'];
    try{
    	$count_docs = $db->executeCount($sel_docs);
    } catch (MySQLException $ex){
    	$ex->alert();
		exit;
	}
    $_i = $_REQUEST['id'];
}
else{
    // nuovo progetto
    $_i = $count_docs = 0;
}

include "dettaglio_progetto.html.php";

?>